import Taro from "@tarojs/taro";
import {useState} from "react";
import { View, Input, Button } from '@tarojs/components'
import {API_URL} from "@/config/url";
import styles from './index.module.scss';

export default function Index() {

  const [account, setAccount] = useState('lisi');
  const [pwd, setPwd] = useState('123456');

  function login(){
    Taro.request({
      url: API_URL+ '/login',
      method: 'POST',
      data: {
        account,
        pwd,
      }
    }).then(res=>{
      if(res.data.data){
        Taro.navigateTo({
          url: '/pages/index/index',
        });
      }else{
        Taro.showToast({
          title: '账号或密码错误',
          icon: 'error',
          duration: 1000
        })
      }
    });
  }

  return (
    <View>
      <h1 className={styles.title}>床位管理系统</h1>
      <View className={styles.loginBox}>
        <Input placeholder='请输入账号' value={account} onInput={e=>setAccount(e.detail.value)} />
        <Input type='password' placeholder='请输入密码' value={pwd} onInput={e=>setPwd(e.detail.value)} />
        <Button type='primary' onClick={login}>登录</Button>
      </View>
    </View>
  )
}
